package com.wuh.controller;

import com.github.pagehelper.PageInfo;
import com.wuh.model.ClassInfor;
import com.wuh.model.SysActive;
import com.wuh.service.ClassInforService;
import com.wuh.service.SysActiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


/**
 * 活动表(SysActive)表控制层
 */

@Controller
public class SysActiveController {
    /**
     * 服务对象
     */
    @Autowired
    private SysActiveService sysActiveService;

    @Autowired
    private ClassInforService classInforService;

    /**
     * 通过主键查询单条数据
     *
     * @param activeId 主键
     * @return 单条数据
     */
    @RequestMapping("selectOne")
    public SysActive selectOne(Integer activeId) {
        return this.sysActiveService.queryById(activeId);
    }


    /**
     * 跳转管理员之课程活动  加载数据  查询操作
     *
     * @return
     */
    @RequestMapping("/querySysActiveAll")
    public String querySysActiveAll(SysActive sysActive, Model model) {
        //加载活动表信息 课程 老师
        List<SysActive> sysActives = sysActiveService.queryAll(sysActive);
        model.addAttribute("sysActives", sysActives);
        //加载课程信息作为创建操作和编辑操作的条件参数
        List<ClassInfor> list = classInforService.queryAll(new ClassInfor());
        model.addAttribute("ClassInforAdmin", list);
        return "system/admin_active";
    }

    /**
     * 跳转管理员之课程活动   创建操作
     *
     * @return
     */
    @RequestMapping("/addSysActive")
    public String addSysActive(MultipartFile upImage, SysActive sysActive) {
        if (!upImage.isEmpty()) {
            //获取项目文件地址
            String filepath = new String("src/main/resources/" + "static/active/img");
            File staticFile = new File(filepath);
            //设置文件名
            String fileName = upImage.getOriginalFilename();  //获取原文件名
            String uuid = UUID.randomUUID().toString().replace("-", ""); //把文件的名称设置唯一值，uuid
            fileName = uuid + "_" + fileName; //文件名
            //图片保存全路径
            String uploadPath = staticFile.getAbsolutePath() + File.separator + fileName;
            File targetFile = new File(uploadPath);
            //上传文件

            if (!targetFile.exists()) {
                targetFile.mkdirs();
                System.out.println("创建新文件");
            }
            try {
                upImage.transferTo(targetFile);  //上传
                System.out.println("上传成功！");
            } catch (IOException e) {
                e.printStackTrace();
            }
            //存储图片访问地址
            fileName = "/active\\img\\" + fileName;
            //设置图片访问地址
            sysActive.setImgUrl(fileName);
            sysActiveService.addActive(sysActive);
            return "redirect:/querySysActiveAll";
        }
        return "redirect:/querySysActiveAll";
    }


    /**
     * 跳转管理员之课程活动   编辑操作
     *
     * @return
     */
    @RequestMapping("/updateSysActive")
    public String updateSysActive(MultipartFile upImage, SysActive sysActive) {
        if (sysActive.getActiveId() != null) {
            if (!upImage.isEmpty()) {
                //获取项目文件地址
                String filepath = new String("src/main/resources/" + "static/active/img");
                File staticFile = new File(filepath);
                //设置文件名
                String fileName = upImage.getOriginalFilename();  //获取原文件名
                String uuid = UUID.randomUUID().toString().replace("-", ""); //把文件的名称设置唯一值，uuid
                fileName = uuid + "_" + fileName; //文件名
                //图片保存全路径
                String uploadPath = staticFile.getAbsolutePath() + File.separator + fileName;
                File targetFile = new File(uploadPath);
                //上传文件
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                    System.out.println("创建新文件");
                }
                try {
                    upImage.transferTo(targetFile);  //上传
                    System.out.println("上传成功！");
                } catch (IOException e) {
                    e.printStackTrace();
                }
                //存储图片访问地址
                fileName = "/active\\img\\" + fileName;
                //设置图片访问地址
                sysActive.setImgUrl(fileName);
                sysActiveService.updateActive(sysActive);
                return "redirect:/querySysActiveAll";
            } else {
                sysActiveService.updateActive(sysActive);
                return "redirect:/querySysActiveAll";
            }
        } else {
            return "redirect:/querySysActiveAll";
        }


    }

    /**
     * 跳转管理员之课程活动   删除操作
     *
     * @return
     */
    @RequestMapping("/deleteSysActive")
    public String deleteSysActive(Integer[] activeIds) {
        for (Integer activeId : activeIds) {
            sysActiveService.deleteById(activeId);
        }
        return "redirect:/querySysActiveAll";
    }

}